package com.xuecheng.teaching.service.impl;

import com.xuecheng.api.comment.model.*;
import com.xuecheng.api.comment.model.dto.CommentDTO;
import com.xuecheng.api.comment.model.dto.ReplyDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.teaching.agent.CommentApiAgent;
import com.xuecheng.teaching.service.TeachingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Service
public class TeachingServiceImpl implements TeachingService {

    @Autowired
    private CommentApiAgent commentApiAgent;

    @Transactional
    @Override
    public CommentDTO createComment(CommentModel model) {
        //1、校验数据
        if (StringUtil.isBlank(model.getStarRank().toString()) || StringUtil.isBlank(model.getTargetId().toString())) {
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        //2、远端调用Feign接口 添加评论
        RestResponse<CommentDTO> response = commentApiAgent.createComment(model);

        if (!response.isSuccessful()) {
            ExceptionCast.cast(CommonErrorCode.E_999978);
        }
        CommentDTO commentDTO = response.getResult();

        return commentDTO;
    }


    @Override
    public PageVO<CommentDTO> querryCommentList(PageRequestParams params, QuerryCommentModel model) {

        //1、校验数据

        Integer pageSize = params.getPageSize();
        if (StringUtil.isNotBlank(pageSize.toString())){
            model.setPageSize(params.getPageSize());
        }
        params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);

        Long pageNo = params.getPageNo();
        if (StringUtil.isNotBlank(pageNo.toString())) {
            model.setPageNo(params.getPageNo());
        }
        params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);

        if (StringUtil.isNotBlank(params.getOrder())) {
            model.setOrder(params.getOrder());
        }
        if (StringUtil.isNotBlank(params.getSortBy())) {
            model.setSortBy(params.getSortBy());
        }

        RestResponse<PageVO<CommentDTO>> response = commentApiAgent.querryCommentList(model);

        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        PageVO<CommentDTO>  pageVO = response.getResult();

        if (ObjectUtils.isEmpty(pageVO)) {
            pageVO = new PageVO(null,0, params.getPageNo(), params.getPageSize());
        }

        return pageVO;
    }


    /**
     * 课程评论统计
     * @param targetId
     * @return
     */
    @Override
    public CommentStaticsModel commentStatistics(Long targetId) {

        RestResponse<CommentStaticsModel>  response = commentApiAgent.commentStatistics(targetId);

        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        CommentStaticsModel resultModel = response.getResult();

        return resultModel;
    }

    //课程评论点赞
    @Transactional
    @Override
    public Boolean praiseComment(Long commentId, String userName) {

        if (StringUtil.isBlank(commentId.toString())||StringUtil.isBlank(userName)){
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }

        RestResponse<Boolean> response = commentApiAgent.praiseComment(commentId, userName);

        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        Boolean result = response.getResult();

        return result;
    }

    //机构的评论列表
    @Override
    public PageVO querryOrgCommentList(PageRequestParams params, QuerryCommentModel model, Long companyId) {
        Long pageNo = params.getPageNo();
        Integer pageSize = params.getPageSize();
        String order = params.getOrder();
        String sortBy = params.getSortBy();
        if (StringUtil.isNotBlank(pageNo.toString())){
            model.setPageNo(pageNo);
        }
        if (StringUtil.isNotBlank(pageSize.toString())){
            model.setPageSize(pageSize);
        }
        if (StringUtil.isNotBlank(order)){
            model.setOrder(order);
        }
        if (StringUtil.isNotBlank(sortBy)){
            model.setSortBy(order);
        }
        if (StringUtil.isNotBlank(companyId.toString())){
            model.setCompanyId(companyId);
        }

        RestResponse<PageVO> response = commentApiAgent.querryOrgCommentList(model);

        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        PageVO<List<CommentDTO>> pageVO = response.getResult();
        return pageVO;
    }


    //评论回复
    @Transactional
    @Override
    public ReplyDTO commentReply(ReplyModel model) {

        RestResponse<ReplyDTO> response = commentApiAgent.commentReply(model);

        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        ReplyDTO replyDTO = response.getResult();

        return replyDTO;
    }

    //评论删除
    @Transactional
    @Override
    public Boolean deleteCommentById(Long commentId) {

        RestResponse<Boolean> response = commentApiAgent.deleteCommentById(commentId);

        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        Boolean result = response.getResult();

        return result;
    }

    //批量删除
    @Override
    public Boolean deleteComments(String[] commentIds) {
        RestResponse<Boolean> response = commentApiAgent.deleteComments(commentIds);
        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        Boolean result = response.getResult();

        return result;
    }

    //运营平台所有 评论列表
    @Override
    public PageVO<List<CommentDTO>> querryOrgAllCommentList(PageRequestParams params) {
        QuerryCommentModel model = new QuerryCommentModel();

        Long pageNo = params.getPageNo();
        Integer pageSize = params.getPageSize();
        String order = params.getOrder();
        String sortBy = params.getSortBy();

        if (StringUtil.isNotBlank(pageNo.toString())){
            model.setPageNo(pageNo);
        }
        if (StringUtil.isNotBlank(pageSize.toString())){
            model.setPageSize(pageSize);
        }
        if (StringUtil.isNotBlank(order)){
            model.setOrder(order);
        }
        if (StringUtil.isNotBlank(sortBy)){
            model.setSortBy(order);
        }

        RestResponse<PageVO> response = commentApiAgent.querryOrgAllCommentList(model);

        if (!response.isSuccessful()){
            ExceptionCast.cast(CommonErrorCode.E_403001);
        }
        PageVO<List<CommentDTO>> pageVO = response.getResult();
        return pageVO;
    }
}
